(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.job.deleteVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'jobService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, jobService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.job = {};
		ctrl.jobOld = {};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobDepency.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.job.jobDepencyList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			ctrl.jobParmetesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobParameters.tableConfigs.table);
			ctrl.jobParmetesOptions.data = 'vm.job.jobParmetes';
			ctrl.jobParmetesOptions.enableRowHeaderSelection = false;
			jobService.initDataOptions(ctrl);
			authService.deleteVerify(params).then(function(response) {
				ctrl.job = response.data.model || {};
				ctrl.job.taskId = response.data.task.taskId;
				ctrl.job.jobDepencyList = ctrl.job.jobDepencyList || [];
			});
		};

		ctrl.deleteVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.job.comments = comments;
						authService.deleteVerifyReject(ctrl.job).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.deleteVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.job.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.deleteVerifyApprove(ctrl.job).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.job.list',
					'state' : 'cda.job.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.job.list',
					'state' : 'cda.job.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);